package org.budo.graph.entity;

import java.util.LinkedHashMap;
import java.util.Map;

import org.apache.commons.lang3.math.NumberUtils;

import lombok.Setter;

import lombok.Getter;

/**
 * @author li
 */
@Getter
@Setter
public class UrlGraph {
    public static final String GRAPH_TYPE = "URL";

    private static final String REQUEST_COUNT = "RequestCount", REQUEST_TIME_MILLIS = "RequestTimeMillis";

    public static final String[] TITLES = { REQUEST_COUNT, REQUEST_TIME_MILLIS };

    private Long requestCount;

    private Long requestTimeMillis;

    public UrlGraph(Map<String, Object> uriStatMap) {
        Object requestCount = uriStatMap.get(REQUEST_COUNT);
        this.setRequestCount(NumberUtils.toLong(requestCount + "", 0L));

        Object requestTimeMillis = uriStatMap.get(REQUEST_TIME_MILLIS);
        this.setRequestTimeMillis(NumberUtils.toLong(requestTimeMillis + "", 0L));
    }

    public Map<String, String> minus(UrlGraph lastUrlGraph) {
        Map<String, String> minusMap = new LinkedHashMap<String, String>(); // 这里应该是需要有序的Map的，顺序应该和 TITLES 一致
        minusMap.put(REQUEST_COUNT, (this.getRequestCount() - lastUrlGraph.getRequestCount()) + ""); // 这里存储的是增量
        minusMap.put(REQUEST_TIME_MILLIS, (this.getRequestTimeMillis() - lastUrlGraph.getRequestTimeMillis()) + ""); // 这里存储的是增量
        return minusMap;
    }
}